package com.wonders.common.parse.jaxb.adapter;

import javax.xml.bind.annotation.adapters.XmlAdapter;

/**
 * 字符CDATA适配器
 * 
 * @author WuBin
 * 
 */
public class CDATAAdapter extends XmlAdapter<String, String> {

	public static final String cdataPrefix = "<![CDATA[";

	public static final String cdataPostFix = "]]>";

	public static final Integer subIndex = cdataPrefix.length();

	@Override
	public String marshal(String value) throws Exception {
		StringBuilder sb = new StringBuilder(cdataPrefix);
		sb.append(value).append(cdataPostFix);
		return sb.toString();

	}

	@Override
	public String unmarshal(String arg0) throws Exception {
		String s = arg0;
		if (arg0.startsWith(cdataPrefix)) {
			s = arg0.substring(subIndex);
			int tailIndex = s.lastIndexOf(cdataPostFix);
			s = s.substring(0, tailIndex);
		}
		return s;
	}

}